/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package blink.view.task;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import javax.swing.JFormattedTextField;

/**
 *
 * @author jerome
 */
public class IntArrayFormat extends JFormattedTextField.AbstractFormatter{

    @Override
    public Object stringToValue(String string) throws ParseException {
        String[] tab = string.split(" *, *");
        int[] res = new int[tab.length];
        for (int i=0 ; i<tab.length ; i++) {
            try {
                res[i]=Integer.parseInt(tab[i]);
            }
            catch (NumberFormatException e) {
                throw new ParseException(tab[i]+ " is not a number",i);
            }
        }
        return res;
    }

    @Override
    public String valueToString(Object o) throws ParseException {
        if(o==null) return "";
        StringBuffer sb = new StringBuffer();
        int[] tab = (int[])o;
        for (int i : tab) {
            sb.append(i);
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        //pos.
        return sb.toString();
    }
    
}
